=begin
  MandelbrotPng.rb
  Calcule une image d'un ensemble de Mandelbrot est l'enregistre au format PNG

  Copyright (C) 2007 by Andrea Blankenstijn
  <darkan9el@gmail.com>

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=end

# Incroyablement simple d'usage en comparaison à libpng...
require 'png'

# Configuration
  ITER    = 100               # nombre maximal d'itérations de la formule
  RES     = 400               # largeur de l'image
  COLORS  = 100               # nombre de couleurs
  CR1     = -2                # valeur initiale de Re(c)
  CR2     = 0.6               # valeur finale de Re(c)
  CI1     = -1                # valeur initiale de Im(c)
  CI2     = 1                 # valeur finale de Im(c)
  FILE    = 'mandelbrot.png'  # nom de fichier

class Mandelbrot
public

  def initialize(res, colors, maxIter, cr1, cr2, ci1, ci2, file)
    @res     = res
    @colors  = colors
    @maxIter = maxIter
    @cr1     = cr1
    @cr2     = cr2
    @ci1     = ci1
    @ci2     = ci2
    @file    = file
    @multip  = 255 / (@colors - 1)
    @step    = (@cr2 - @cr1) / @res
    @rapport = (@ci2 - @ci1) / (@cr2 - @cr1)
    @height  = (@res * @rapport).ceil
    @canvas  = PNG::Canvas.new @res, @height

    IterateFormula()
    SavePng()
    puts 'Image ' << @file << ' générée'
  end

private

  @res      # résolution
  @colors   # nombre de couleurs
  @maxIter  # maximum d'itération
  @cr1      # valeur initiale de Re(c)
  @cr2      # valeur finale de Re(c)
  @ci1      # valeur initiale de Im(c)
  @ci2      # valeur finale de Im(c)
  @file     # nom de fichier
  @canvas   # image de travail

  def IterateFormula
    cr = @cr1
    for x in 0...@res
      PrintProgress(x)
      ci = @ci2 # On va de bas en haut, donc du positif vers le négatif de l'axe imaginaire
      for y in 0...@height
        zr = 0
        zi = 0
        for i in 0...@maxIter + 1
          tmp = zr
          zr2 = zr ** 2
          zi2 = zi ** 2

          zr  = zr2 - zi2 + cr
          zi  = 2 * tmp * zi + ci

          if (zr2 + zi2) > 4
            @canvas[x, y] = PNG::Color.from '0x' << ZeroComplete((i % @colors).to_s(base=16)) << 'FF'
            break
          end
          if i == @maxIter
            @canvas[x, y] = PNG::Color::Black
            break
          end
        end
        ci -= @step
      end
      SavePng()
      cr += @step
    end
  end

  def SavePng
    png = PNG.new @canvas
    png.save @file
  end

  def ZeroComplete(hexNb)
    for i in 0...(6 - hexNb.length)
      hexNb =  '0' << hexNb
    end
    return hexNb
  end

  def PrintProgress(status)
    progress = (status.to_f / @res.to_f * 100).to_s << '%'
    puts progress # "\b" * progress.length << "\b" * progress.length <<
  end

end

Mandelbrot.new(RES, COLORS, ITER, CR1, CR2, CI1, CI2, FILE)
